package org.example;

import java.util.ArrayList;
import java.util.Scanner;


public class StudentSystem {
    public static void startStudentSystem() {
        ArrayList<Student> list =new ArrayList<>();
       loop:while (true) {
            System.out.println("-------欢迎来到学生系统-------");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.修改学生");
            System.out.println("4.查询学生");
            System.out.println("5.退出");
            System.out.println("请输入你的选择:");
            Scanner sc = new Scanner(System.in);
            switch (sc.next()) {
                case "1"->addStudent(list);
                case "2"->deleteStudent(list);
                case "3"->updateStudent(list);
                case "4"->queryStudent(list);
                case "5"->{
                    System.out.println("退出");
                   // break  loop;
                    System.exit(0);
                }
                default-> System.out.println("输入有误");
            }
        }
    }
    //添加学生
    public static void addStudent(ArrayList<Student> list){
        System.out.println("添加学生:");
        Student s = new Student();
        Scanner sc=new Scanner(System.in);
        while (true) {
            System.out.println("请输入学生学号:");
            String id=sc.next();
            boolean flag=contains(list,id);
            if(flag){
                System.out.println("学号已存在，请重新输入");
                return;
            }else{
                s.setId(id);
                break;
            }
        }
        System.out.println("请输入学生姓名:");
        String name =sc.next();
        s.setName(name);
        System.out.println("请输入学生年龄:");
        s.setAge(sc.nextInt());
        System.out.println("请输入学生家庭住址:");
        s.setAddress(sc.next());

        list.add(s);

        System.out.println("添加学生成功");
    }
    //删除学生
    public static void deleteStudent(ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("所要删除学生的ID:");
        String id=sc.next();
        int Index=getIndex(list,id);
        if(Index>=0){
            list.remove(Index);
            System.out.println("id为"+id+"的学生删除成功");
        }else{
            System.out.println("该学生不存在，请重新输入");
        }
    }
    //修改学生
    public static void updateStudent(ArrayList<Student> list){
        System.out.println("修改学生：");
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要修改的学生ID:");
        String id=sc.nextLine();
        int Index=getIndex(list,id);
        if(Index==-1){
            System.out.println("该学生不存在，请重新输入");
            return;
        }
        //获取要修改的学生对象
        Student s=list.get(Index);
        System.out.println("请输入新学生学号:");
        String newid=sc.next();
        s.setId(newid);
        System.out.println("请输入新学生姓名:");
        String newname=sc.next();
        s.setName(newname);
        System.out.println("请输入新学生年龄:");
        int newage=sc.nextInt();
        s.setAge(newage);
        System.out.println("请输入新学生家庭住址:");
        String newaddress=sc.next();
        s.setAddress(newaddress);
        System.out.println("新学生修改成功");
    }
    //查询学生
    public static void queryStudent(ArrayList<Student> list){
        if(list.size()==0){
            System.out.println("无信息，请先添加信息");
            return;
        }
        System.out.println("学号\t\t姓名\t\t年龄\t\t家庭住址");
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            System.out.println(s.getId()+"\t\t"+s.getName()+"\t\t"+s.getAge()+"\t\t"+s.getAddress());
        }
    }

    public static boolean contains(ArrayList<Student> list,String id){
        boolean flag=false;
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if(s.getId().equals(id)){
                flag=true;
                break;
            }
        }
        return flag;
    }

    public static int getIndex(ArrayList<Student> list,String id){
        for (int i = 0; i < list.size(); i++) {
            Student s = list.get(i);
            if(s.getId().equals(id)){
                   return i;
            }
        }
        return -1;
        }
}
